<?php
include 'core/initialize.php';
logged_in_redirect();

if (empty ($_POST)=== false){
   $required_fields = array('Username','Password','Password_again','First_name','Last_name','User_email','CCard', 'Gender', 'Age', 'Address');
   foreach($_POST as $key =>$value){
    if (empty ($value) && in_array($key, $required_fields) ===true){
        $errors[] = 'Fields marked with an asterisk are required!';
        break 1;
        
    }
   }
   if(empty($errors) === true){
     if(user_exists ($_POST['Username']) ===true){
        $errors[]='Sorry, the username \''.$_POST['Username']. '\' is already taken.';
     }
     if(preg_match("/\\s/",$_POST['Username'])==true){
        $errors[] = 'Your username must not contain spaces';
     }
     if(strlen($_POST['Password']) <6 ){
        $errors[]= 'Your password must be atleast 6 characters';
     }
     if($_POST['Password']!==$_POST['Password_again']){
        $errors[] = 'Your password do not match';
     }
     if(filter_var ($_POST['User_email'], FILTER_VALIDATE_EMAIL)===false){
        $errors[] = 'A valid Email Address is required';
     }
    if(email_exists($_POST['User_email'])===true){
        $errors[]='Sorry, the Email address \''.$_POST['User_email']. '\' is already in use.';
    }
    
   }
}

?>
<div id="center">
<div class="boxed">
<h1 class="heading">Registration</h1>
<?php
if(isset($_GET['success'])&& empty($_GET['success'])){
   echo 'You\'ve Been Registered Successfully';
}else{

if(empty($_POST)=== false && empty($errors)===true){
    $register_data = array(
      'Username'=>$_POST['Username'],
      'Password'=>$_POST['Password'],
      'First_name'=>$_POST['First_name'],
      'Last_name'=>$_POST['Last_name'],
      'User_email'=>$_POST['User_email'],
      'CCard'=>$_POST['CCard'],
      'Gender'=>$_POST['Gender'],
      'Age'=>$_POST['Age'],
      'Address'=>$_POST['Address']
      );
    register_user($register_data);
    header('Location: register.php?success');
    exit();

}else if(empty($errors)=== false){
    echo output_errors($errors);
}

?>
<form action= "" method="post">
    <ul>
        Username*:<br>
        <input type="text" name="Username"><br><br>
        Password*:<br>
        <input type="password" name="Password"><br><br>
        Re-type Password*:<br>
        <input type="password" name="Password_again"><br><br>
        First Name*:<br>
        <input type="text" name="First_name"><br><br>
        Last Name*:<br>
        <input type="text" name="Last_name"><br><br>
        Email address*:<br>
        <input type="text" name="User_email"><br><br>
        Credit Card No.*:<br>
        <input type="text" name="CCard"><br><br>
        
        Gender.*:<br>
        <input type="radio" name="Gender" value="male">Male<br>
        <input type="radio" name="Gender" value="female">Female<br><br>
        
        Age*:<br>
        <input type="int" name="Age"><br><br>
       
        Address*:<br>
        <input type="text" name="Address"><br>
        <br>
       
        <input type="submit" value="Register">
        
    
    </ul>
</form>
</div>
</div>
<?php
}
?>